/*  
 *  Copyright (c) 2008 Adrian Macneil
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

const asHost = "localhost";
const asPort = 33367;
const asPluginId = "tst";

var sbScrobbler = sbScrobbler ? sbScrobbler : {
  onLoad: function() {
    // observe changes to play status
    gPPS.addListener(sbScrobbler.playObserver);
    
    // observe changes to pause status
    var pausedRemote = SB_NewDataRemote("faceplate.paused", null);
    pausedRemote.bindObserver(sbScrobbler.pauseObserver, true);
    
    // TODO: try launching the last.fm client
  }
}

sbScrobbler.commands = {
  sendCommand: function(cmd) {
    cmd += "\n"; // vital detail
    
    // get the transport
    var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"]
          .getService(Components.interfaces.nsISocketTransportService);
    var transport = transportService.createTransport(null, 0, asHost, asPort, null);    
    
    // write to the stream
    var outstream = transport.openOutputStream(0, 0, 0);
    outstream.write(cmd, cmd.length);
    outstream.close();
  },

  escapeString: function(cmd) {
    if (cmd == null) return "";
    
    // we need to escape ampersands
    return cmd.replace("&", "&&");
  },

  cmdStart: function(artist, track, album, time, filename) {
    // called when a song starts playing
    var cmd = "START c=" + asPluginId +
              "&a=" + sbScrobbler.commands.escapeString(artist) +
               "&t=" + sbScrobbler.commands.escapeString(track) +
               "&b=" + sbScrobbler.commands.escapeString(album) +
               "&l=" + time +
               "&p=" + sbScrobbler.commands.escapeString(filename);
    sbScrobbler.commands.sendCommand(cmd);
  },

  cmdStop: function() {
    // called when a song is stopped (manually or endsong)
    var cmd = "STOP c=" + asPluginId;
    sbScrobbler.commands.sendCommand(cmd);
  },

  cmdPause: function() {
    // called when a song is manually paused
    var cmd = "PAUSE c=" + asPluginId;
    sbScrobbler.commands.sendCommand(cmd);
  },

  cmdResume: function() {
    // called after a pause to resume playback
    var cmd = "RESUME c=" + asPluginId;
    sbScrobbler.commands.sendCommand(cmd);
  }
}

sbScrobbler.playObserver = {
  onBeforeTrackChange: function() { },  

  onTrackChange: function() {
    // get some info
    var mediaItem = gPPS.playingView.mediaList.getItemByGuid(gPPS.currentGUID);
    
    var artist = mediaItem.getProperty("http://songbirdnest.com/data/1.0#artistName");
    var track = mediaItem.getProperty("http://songbirdnest.com/data/1.0#trackName");
    var album = mediaItem.getProperty("http://songbirdnest.com/data/1.0#albumName");
    var url = gPPS.core.getMetadata("url");
    var time = mediaItem.getProperty("http://songbirdnest.com/data/1.0#duration");
    time = parseInt(time/1000000); // in seconds, please

    // send a start command
    sbScrobbler.commands.cmdStart(artist, track, album, time, url);
  },
  
  onStop: function() {
    // send a stop command
    sbScrobbler.commands.cmdStop();
  }
}

sbScrobbler.pauseObserver = {
  observe: function(aSubject, aTopic, aData) {
    if (aTopic == "faceplate.paused") {
      if (aData == 1) { // a track has been paused
        sbScrobbler.commands.cmdPause();
      }
      else { // a track has been resumed
        sbScrobbler.commands.cmdResume();
      }
    }
  }
}

sbScrobbler.onLoad();
